{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*\n",
    "import wenxin_api # 可以通过\"pip install wenxin-api\"命令安装\n",
    "from wenxin_api.tasks.free_qa import FreeQA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append(os.path.abspath('../..'))\n",
    "\n",
    "from local_pwd import wenxin_api_ak, wenxin_api_sk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "wenxin_api.ak = wenxin_api_ak\n",
    "wenxin_api.sk = wenxin_api_sk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "input_dict = {\n",
    "    \"async\": 1,\n",
    "    \"text\": \"问题：交朋友的原则是什么？\\n回答：\",\n",
    "    \"min_dec_len\": 2,\n",
    "    \"seq_len\": 512,\n",
    "    \"topp\": 0.5,\n",
    "    \"penalty_score\": 1.2,\n",
    "    \"min_dec_penalty_text\": \"。?：！[<S>]\",\n",
    "    \"is_unidirectional\": 0,\n",
    "    \"task_prompt\": \"qa\",\n",
    "    \"mask_type\": \"paragraph\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024-06-01 09:52:30,633 - model 1: starts writing\n"
     ]
    },
    {
     "ename": "APIError",
     "evalue": "{\n  \"msg\": \"No message available\",\n  \"code\": 1\n}",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "File \u001b[0;32m~/anaconda3/envs/llm/lib/python3.10/site-packages/wenxin_api/tasks/free_qa.py:45\u001b[0m, in \u001b[0;36mFreeQA.create\u001b[0;34m(cls, text, model, **params)\u001b[0m\n\u001b[1;32m     44\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 45\u001b[0m     task_id \u001b[38;5;241m=\u001b[39m \u001b[43mresp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mjson\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mdata\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtaskId\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m     46\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n",
      "\u001b[0;31mKeyError\u001b[0m: 'data'",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mAPIError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[16], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m rst \u001b[38;5;241m=\u001b[39m \u001b[43mFreeQA\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mcreate\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43minput_dict\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m      2\u001b[0m \u001b[38;5;28mprint\u001b[39m(rst)\n",
      "File \u001b[0;32m~/anaconda3/envs/llm/lib/python3.10/site-packages/wenxin_api/tasks/free_qa.py:47\u001b[0m, in \u001b[0;36mFreeQA.create\u001b[0;34m(cls, text, model, **params)\u001b[0m\n\u001b[1;32m     45\u001b[0m     task_id \u001b[38;5;241m=\u001b[39m resp\u001b[38;5;241m.\u001b[39mjson()[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdata\u001b[39m\u001b[38;5;124m\"\u001b[39m][\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mtaskId\u001b[39m\u001b[38;5;124m\"\u001b[39m]\n\u001b[1;32m     46\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m---> 47\u001b[0m     \u001b[38;5;28;01mraise\u001b[39;00m APIError(json\u001b[38;5;241m.\u001b[39mdumps(resp\u001b[38;5;241m.\u001b[39mjson(), \n\u001b[1;32m     48\u001b[0m                    ensure_ascii\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m,\n\u001b[1;32m     49\u001b[0m                    indent\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2\u001b[39m))\n\u001b[1;32m     51\u001b[0m not_ready \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[1;32m     52\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m not_ready:\n",
      "\u001b[0;31mAPIError\u001b[0m: {\n  \"msg\": \"No message available\",\n  \"code\": 1\n}"
     ]
    }
   ],
   "source": [
    "rst = FreeQA.create(**input_dict)\n",
    "print(rst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
